﻿using BepInEx;
using Config;
using GenUI.MiniGame;
using HarmonyLib;
using MiniGame.Quiz;
using Sdk;
using System;
using System.Runtime.Remoting.Messaging;
using UnityEngine;
using UnityEngine.UI;
using View.Event;

namespace AnswerTips
{
    [BepInPlugin("AnswerTips", "AnswerTip", "0.0.1")]
    public class AnswerTip : BaseUnityPlugin
    {
        private void Start()
        {
            Harmony.CreateAndPatchAll(typeof(AnswerTip), null);
        }

        [HarmonyPostfix]
        [HarmonyPatch(typeof(QuizMiniGameView), "OnRenderAnswer")]
        public static void OnRenderAnswerPath(QuizMiniGameView __instance, UICell _cell)
        {
            Cell_QuizItemUI cell_QuizItemUI = _cell as Cell_QuizItemUI;
            int key = (int)Traverse.Create(__instance).Field("questionId").GetValue();
            int answer = Cfg.QuizCompetitionCfgMap[key].answer;
            if ((int)cell_QuizItemUI.data == answer)
            {
                cell_QuizItemUI.txt_option.color = Color.red;
                cell_QuizItemUI.txt_content.color = Color.red;
            }
        }


        [HarmonyPostfix]
        [HarmonyPatch(typeof(StateEvtView), "HelpShowTalk")]
        public static void HelpShowTalkPath(StateEvtView __instance, int _talkId, string _txt = null)
        {
            TalkCfg talkCfg = Cfg.TalkCfgMap[_talkId];
            if (talkCfg.option != null && talkCfg.option.Count != 0)
            {
                int num = 0;
                foreach (var option in talkCfg.option)
                {
                    var id = Cfg.OptionCfgMap[option].talkId;
                    if (id.Count == 1 && id[0] > 1)
                    {
                        var nextid = Cfg.TalkCfgMap[id[0]].nextTalk;
                        if (nextid.Count == 1 && nextid[0] > 1)
                        {
                            if (Cfg.TalkCfgMap[nextid[0]].effect.Count > 0)// 正确答案
                            {
                                var itemgroup = (UIItemGroup)Traverse.Create(__instance).Field("itemgroup_option").GetValue();
                                var listcell = itemgroup.GetCells();
                                if (listcell.Count >= num)
                                {
                                    var text = listcell[num].gameObject.GetComponents<Text>();
                                    foreach( var item in text)
                                    {
                                        item.color = Color.red;
                                    }
                                }
                                // this.itemgroup_option.gameObject
                            }
                        }
                    }
                    num++;
                }
            }
        }
    }
}
